package com.ad.pages;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.tapestry5.annotations.Id;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.RequestGlobals;
import org.guzz.service.dir.impl.LocationResult;

import com.ad.base.AbstractPage;
import com.ad.hibernate.TipView;
import com.ad.services.IHttpService;
import com.ad.services.ITipManagerService;
import com.ad.system.Common;

public class Home extends AbstractPage {
	private static Logger log = Logger.getLogger(Home.class);

	@Inject
	private RequestGlobals request;

	@Inject
	@Id("httpService")
	private IHttpService httpService;

	@Inject
	@Id("tipService")
	private ITipManagerService tipService;

	@Property
	private String ip;

	@SuppressWarnings("unused")
	@Property
	private String locationInfo;

	@SuppressWarnings("unused")
	@Property
	private String script;

	@SuppressWarnings("unused")
	@Property
	private String fun;

	@SuppressWarnings("unused")
	@Property
	private String fun2;

	private boolean locationSubmit;

	@Property
	private List<TipView> tips;

	@Property
	private TipView tip;

	@SuppressWarnings("unused")
	@Property
	@Persist("session")
	private String userPetName;

	protected void setupRender() {
		super.setupRender();
		tips = this.tipService.findTipViews();
		if (!Common.isEmpty(tips)) {
			generateScript();
		}
	}

	void onActivate(String value) {
		log.info(value);
	}

	private void generateScript() {
		// List<String> divIds = new ArrayList<String>();
		// for (int i = 0; i < tips.size(); i++) {
		// Tip tip = tips.get(i);
		// // tip.addStyle(CSSDefault.getRamdonNumber());
		// divIds.add(tip.getDivId());
		// }
		// divIds.add("weather");
		StringBuffer s = new StringBuffer();
		StringBuffer s2 = new StringBuffer();
		StringBuffer s3 = new StringBuffer();
		StringBuffer temp = new StringBuffer();

		s.append("Drag.initDivs(new Array(");
		s2.append("fadeOutAll('fadeOut',1000,new Array(");
		s3.append("fadeOutAll('fadeIn',1000,new Array(");

		for (int i = 0; i < tips.size() - 1; i++) {
			TipView tip = tips.get(i);
			temp.append("'").append(tip.getId()).append("',");
		}
		tip = tips.get(tips.size() - 1);

		// for (int i = 0; i < divIds.size() - 1; i++) {
		// temp.append("'").append(divIds.get(i)).append("',");
		// }
		temp.append("'").append(tip.getId()).append("'));");

		script = s.append(temp).toString();
		fun = s2.append(temp).toString();
		fun2 = s3.append(temp).toString();
	}

	protected Object onActivate() {
		return null;
	}

	void onSelectedFromlocation() {
		locationSubmit = true;
	}

	// Object onSuccess() {
	// if (locationSubmit && !Common.isEmpty(ip)) {
	// LocationResult l = httpService.findLocation(ip, request
	// .getHTTPServletRequest());
	// locationInfo = l.cityName + l.detailLocation;
	// }
	// return null;
	// }

}
